<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .kb {
            display: flex;
            flex: 1;
            flex-wrap: row nowrap;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }

        h1 {
            font-weight: normal;
        }

        .key {
            width: 100px;
            margin: 1rem;
            padding: 1rem .5rem;
            font-size: 1rem;
            text-align: center;
            text-shadow: 0 0 5px black;
            background-color: #999999;
            color: white;
            border: 4px solid black;
            border-radius: 5px;
            transition: all .07s;
        }

        .play {
            transform: scale(1.1);
            border-color: #ffc04e;
            box-shadow: 0 0 10px #ffc04e;
        }

        span {
            color: #ffc04e;
            font-size: 12px;
        }
    </style>
</head>

<body>
    <div class="kb">
        <div data-key="65" class="key">
            <h1>A</h1>
            <span>CLAP</span>
        </div>
        <div data-key="83" class="key">
            <h1>S</h1>
            <span>HIHAT</span>
        </div>
        <div data-key="68" class="key">
            <h1>D</h1>
            <span>KICK</span>
        </div>
        <div data-key="70" class="key">
            <h1>F</h1>
            <span>OPENHAT</span>
        </div>
        <div data-key="71" class="key">
            <h1>G</h1>
            <span>BOOM</span>
        </div>
        <div data-key="72" class="key">
            <h1>H</h1>
            <span>RIDE</span>
        </div>
        <div data-key="74" class="key">
            <h1>J</h1>
            <span>SNARE</span>
        </div>
        <div data-key="75" class="key">
            <h1>K</h1>
            <span>TOM</span>
        </div>
        <div data-key="76" class="key">
            <h1>L</h1>
            <span>TINK</span>
        </div>
    </div>
    <audio data-key="65" src="sounds/clap.wav"></audio>
    <audio data-key="83" src="sounds/hihat.wav"></audio>
    <audio data-key="68" src="sounds/kick.wav"></audio>
    <audio data-key="70" src="sounds/openhat.wav"></audio>
    <audio data-key="71" src="sounds/boom.wav"></audio>
    <audio data-key="72" src="sounds/ride.wav"></audio>
    <audio data-key="74" src="sounds/snare.wav"></audio>
    <audio data-key="75" src="sounds/tom.wav"></audio>
    <audio data-key="76" src="sounds/tink.wav"></audio>

    <script>
        function kit(event) {
            const key = document.querySelector(`div[data-key="${event.keyCode}"]`);
            const audio = document.querySelector(`audio[data-key="${event.keyCode}"]`);
            if (!audio) return;
            key.classList.add('play');
            audio.currentTime = 0; // 每次播放之后都使音频播放进度归零
            audio.play();
        }

        function removeTransition(event) {
            if (event.propertyName !== 'transform') return; // 过滤其中一种事件
            event.target.classList.remove('play');
        }
        const keys = Array.from(document.querySelectorAll('.key'));
        keys.forEach(key => key.addEventListener('transitionend', removeTransition));
        window.addEventListener('keydown', kit);

    </script>
</body>

</html>